package search;

import java.util.ArrayList;

public class BetweenTextSearch extends Search {
	
	private int severity = 0; //What matches are we looking for?
	private ArrayList<app.Essay> essays = new ArrayList<app.Essay>();
	
	public BetweenTextSearch(int severity, ArrayList<app.Essay> essays)
	{
		super(essays);
		this.severity = severity;
		this.essays = essays;
	}
	
	public ArrayList<BetweenResult> search()
	{
		System.out.println("Start the search!");
		ArrayList<BetweenResult> results = new ArrayList<BetweenResult>();
		for(int i=0;i<essays.size();i++){ //For each essay
			for(int j=0;j<essays.size();j++){ //Check each essay
				
				app.Essay pri = (app.Essay)essays.get(i); //The primary (one we're checking)
				int priLength = pri.sentences.size();	//The length of the first essay
				app.Essay sec = (app.Essay)essays.get(j); //The secondary (one we're checking against)
				int secLength = sec.sentences.size();	//The length of the second essay
				
				if((pri.id != sec.id)){
				
					for(int k=0;k<priLength;k++){	//For each sentence in the first
						System.out.println(pri.getSentence(k));
						for(int l=0;l<secLength;l++){	//Check each sentence in the second
							System.out.println("\t" + sec.getSentence(l));
							int flags = 0;	//The number of words flagged as duplicate
							
							//Split the sentences into words
							String[] priWords = pri.getSentence(k).split(" ");
							String[] secWords = sec.getSentence(l).split(" ");
														
							//Check all secondary words to see if they match
							for(int m=0;m<priWords.length;m++){	//For each word in the first
								if(priWords[m].length() > 3){
									for(int n=0;n<secWords.length;n++){	//Check each word in the second
										if(priWords[m].equals(secWords[n])){								
											//We've matched a word
											System.out.println("Match!");
											flags++;
										} else 
											System.out.println("Not matched!");
									}
								}
							}
							//We've finished an iteration through a sentence, see if it needs flagged
							int result;
							if(flags == 0)
								result = 0;
							else
								result = Math.round((secWords.length / flags) * 100);
							
							//Sort longer primary sentences out
							if(priWords.length > secWords.length){
								int correction = Math.round((priWords.length / secWords.length) * 100);
								result -= correction;
							}
							
							switch(severity){
							case 0:
								//100% match
								if(result == 100)
									results.add(new BetweenResult(k,pri.getSentence(k),false,sec.id,l));
								else
									results.add(new BetweenResult(k,pri.getSentence(k),true,-1,-1));
							break;
							case 1:
								//75% match
								if(result > 75)
									results.add(new BetweenResult(k,pri.getSentence(k),false,sec.id,l));
								else
									results.add(new BetweenResult(k,pri.getSentence(k),true,-1,-1));
							break;
							case 2:
								//50% match
								if(result > 50)
									results.add(new BetweenResult(k,pri.getSentence(k),false,sec.id,l));
								else
									results.add(new BetweenResult(k,pri.getSentence(k),true,-1,-1));
							break;
							case 3:
								//25% match
								if(result > 25)
									results.add(new BetweenResult(k,pri.getSentence(k),false,sec.id,l));
								else
									results.add(new BetweenResult(k,pri.getSentence(k),true,-1,-1));
							break;								
							}
						}
					}
				}
				//End of the sentences loops
			}
		}
		System.out.println("I'm finished.");
		return results; 
	}
}
